/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
<script type="text/javascript" src="jquery-1.7.1.js"></script>
function TamVentana() {
  var Tamanyo = [0, 0];
  if (typeof window.innerWidth != 'undefined')
  {
    Tamanyo = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0)
  {
 Tamanyo = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else   {
    Tamanyo = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }
  return Tamanyo;
}

window.onresize = function() {
  var Tam = TamVentana();
  alert('La ventana mide: [' + Tam[0] + ', ' + Tam[1] + ']');
};


	

function TamVentana() {
  var Tamano = [0, 0];
  if (typeof window.innerWidth != 'undefined')
  {
    Tamano = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0)
  {
 Tamano = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else   {
    Tamano = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }
  return Tamano;
}



$(function(){
        var status = document.getElementById("status");

        var socket;

        if (window.hasOwnProperty("WebSocket")) { // webkit-browser
                socket = new WebSocket("ws://127.0.0.1:1990/");
        }
        else if (window.hasOwnProperty("MozWebSocket")) { // firefox 
                socket = new MozWebSocket("ws://127.0.0.1:1990/");
        }
        else { // El explorador no soporta WebSocket
                status.innerHTML = "Tu explorador no soporta web sockets!.";
                return;
        }

        socket.onopen = function() { // El socket esta listo, eviar algo
                status.innerHTML = "Conexion correcta.";
        };

        socket.onmessage = function (evt) {
                status.innerHTML = "Datos obtenidos del Kinect.";

                // Obtener el dato en formato JSON.
                var jsonObject = eval('('+evt.data+')');
                var event = jsonObject.event;
                var x = parseFloat(jsonObject.x);
                var y = parseFloat(jsonObject.y);
                var z = parseFloat(jsonObject.z);
                
                var Tam = TamVentana();
                var xReal = parseFloat(Tam[0]);
                var yReal = parseFloat(Tam[1]);
                
                x = (xReal/640.0)*x;
                y = (yReal/480.0)*y;
                
                switch(event){
                    case 'KeyDown': alert("down");
                    break;
                    case 'KeyUp': alert("up");
                    break;
                    case 'Drag': alert("dag");
                    break;
                    
                        
                }

        };

        socket.onclose = function() { // El servidor cierra la conexion
                status.innerHTML = "Conexion cerrada.";
        };
});


